#include "appviewimpl.h"

CAppViewImpl::CAppViewImpl(void)
{
}
CAppViewImpl::~CAppViewImpl(void)
{
    logi("CAppViewImpl::~CAppViewImpl");
}
HRESULT CAppViewImpl::Init(IBase* pRot, IBase* pBase)
{
    rc_error(pRot != NULL, E_FAIL);
    return pRot->QueryInterface(IID_IRot, (void**)&m_pRot);
}
HRESULT CAppViewImpl::Uninit()
{
    HRESULT hr = S_OK;

    m_pRot.dispose();

    return hr;
}
HRESULT CAppViewImpl::Start(_pid_t pid, unsigned int type)
{
    HRESULT hr = S_OK;

    InitBus();

    _lComPtr<INetFrame> pINet;
    m_pRot->GetObject(CLSID_INetFrame, IID_INetFrame, (IBase**)&pINet.m_p);
    rc_error(pINet.m_p != INULL, E_FAIL);

    hr = pINet->Create(protocol_modbus, (IBase**)&m_pIModBus);
    rc_error(hr == S_OK, E_FAIL);

    // m_pIHttp->Release();
    //_lComPtr<IBase> pBase;
    //  hr = this->QueryInterface(IID_IBase, (void**)&pBase);
    //  rc_error(hr == S_OK, E_FAIL)

    // hr = m_pIHttp->BindEvent(pBase);
    // rc_error(hr == S_OK, E_FAIL)

    // SafeStringPtr addr = {
    //	"110.242.68.3:443",
    //	s_strlen("110.242.68.3:443")
    // };

    // SafeStringPtr scheme = {
    //	"\\\\.\\pipe\\mynamedpipe1",
    //	s_strlen("\\\\.\\pipe\\mynamedpipe1")
    // };

    ////hr = m_pIHttp->Open(addr, scheme, 443);
    ////rc_error(hr == S_OK, E_FAIL)

    //_lComPtr<IPipe> m_IPipe;
    // m_pIAsynFrame->CreateIo(io_pipe, (IBase**)&m_IPipe);

    //_lComPtr<ISPipe> m_ISPipe;
    // m_pIAsynFrame->CreateIo(io_spipe, (IBase**)&m_ISPipe);

    // m_ISPipe->OpenPipe(&scheme);
    // m_ISPipe->WaitPipe();

    // m_IPipe->OpenPipe(&scheme);

    // CSemHandle sem;
    // sem.WaitTime(300);
    // m_pIHttp.dispose();

    return hr;
}
HRESULT CAppViewImpl::Stop(unsigned int exit)
{
    HRESULT hr = S_OK;

    return UnInitBus();
}
HRESULT CAppViewImpl::OnHttpAuth(ULONG event)
{
    return S_OK;
}
HRESULT CAppViewImpl::OnHttpNotify(crtid cid, crterr err)
{
    if (cid == HTTP_IMPL_CONNECTED) {
        logi("OnHttpNotify::Connected");
    }
    return S_OK;
}
HRESULT CAppViewImpl::OnHttpSendParam(UCHAR* key, buf_len klen, UCHAR* value, buf_len vlen)
{
    return S_OK;
}
HRESULT CAppViewImpl::OnHttpRecvParam(UCHAR* key, buf_len klen, UCHAR* value, buf_len vlen)
{
    return S_OK;
}
HRESULT CAppViewImpl::OnHttpSendHeaded(UCHAR* data, buf_len size)
{
    return S_OK;
}
HRESULT CAppViewImpl::OnHttpRecvHeaded(UCHAR* data, buf_len size)
{
    return S_OK;
}
HRESULT CAppViewImpl::OnHttpSendBody(UCHAR* data, buf_len size)
{
    return S_OK;
}
HRESULT CAppViewImpl::OnHttpRecvBody(UCHAR* data, buf_len size)
{
    return S_OK;
}

HRESULT CAppViewImpl::InitBus()
{
    HRESULT hr = S_OK;

    // m_pRot->SendMessage(CLSID_CObjectRun, CLSID_CLoader, 0, 0, NULL);

    hr = m_pRot->GetObject(CLSID_CMsgBus, IID_IMsgBus, (IBase**)&m_pIMsgBus);
    rc_error(hr == S_OK, E_FAIL);

    hr = m_pIMsgBus->CreateNode(NDC_BROKER, (IBase**)&m_pIBroker);
    rc_error(hr == S_OK, E_FAIL);

    m_pIBroker->Listen("127.0.0.1:8080", s_strlen("127.0.0.1:8080"), 5);

    hr = m_pIMsgBus->CreateNode(NDC_CONSUMER, (IBase**)&m_pIConsumer);
    rc_error(hr == S_OK, E_FAIL);
    m_pIConsumer->SetId(_inet_addr_v4("255.254.253.252"));
    // m_pIConsumer->Bind("127.0.0.1:8080", s_strlen("127.0.0.1:8080"));

    hr = m_pIMsgBus->CreateNode(NDC_PRODUCER, (IBase**)&m_pIProducer);
    rc_error(hr == S_OK, E_FAIL);
    m_pIProducer->SetId(_inet_addr_v4("255.254.253.251"));
    // m_pIProducer->Bind("127.0.0.1:8080", s_strlen("127.0.0.1:8080"));

    // hr = m_pIMsgBus->CreateNode(NDC_AGENT, (IBase**)&m_pIAgent);
    // rc_error(hr == S_OK, E_FAIL)
    // m_pIAgent->SetId(_inet_addr_v4("255.254.253.250"));
    // m_pIAgent->Bind("127.0.0.1:8080", s_strlen("127.0.0.1:8080"));

    int sec = 0;
#if (TARGET_OS == OS_WIN)
    sec = 0;
#elif (TARGET_OS == OS_POSIX)
    sec = 10;
#elif (TARGET_OS == OS_MACH)
    sec = 10;
#endif
    return m_pIBroker->Dispatch(sec);
}
HRESULT CAppViewImpl::UnInitBus()
{
    if (m_pIBroker.m_p != NULL) {
        m_pIBroker->Close();
        m_pIBroker.dispose();
    }

    if (m_pIConsumer.m_p != NULL) {
        m_pIConsumer->Close();
        m_pIConsumer.dispose();
    }

    if (m_pIProducer.m_p != NULL) {
        m_pIProducer->Close();
        m_pIProducer.dispose();
    }

    if (m_pIAgent.m_p != NULL) {
        m_pIAgent->Close();
        m_pIAgent.dispose();
    }

    m_pIMsgBus.dispose();

    return S_OK;
}
